<?php

namespace App\Http\Controllers\admin;
use App\Http\Model\Goods;
use App\Http\Model\Permission_group;
use App\Http\Model\RolePermission;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
/**
 * Class IndexController 后台首页控制器
 * @package App\Http\Controllers\admin
 */
class IndexController extends AdminPlatformController
{

    /**
     * 引入后台模板 index方法
     * @return 后台模板
     */
    public function index(){
        return view('admin.index.index');
    }

    /**
     *引入后台主体方法
     */
    public function main(){
        //dd(phpinfo());
        $data = Goods::all();
        $arr = Goods::where('is_on_sale','1')->get();
        $number = Goods::where('goods_number','1')->get();
        $delete = Goods::where('is_delete','1')->get();
        $best = Goods::where('is_best','1')->get();
        $hot = Goods::where('is_hot','1')->get();
        return view('admin.index.main',compact('data','arr','number','delete','best','hot'));
    }
    /**
     *引入后台左侧导航方法
     */
    public function menu(){
        /** 根据管理员权限生成动态导航菜单 */
        $menu = $this -> initMenu();
        /** 加载功能菜单视图 */
        return view('admin.index.menu',compact('menu'));
    }
    /**
     *引入后台头部方法
     */
    public function top(){
        return view('admin.index.top');
    }

    /**
     *后台登录get请求方法
     *@return 后台登录模板
     */
    public function login(){
        return view('admin.privilege.login');
    }

    /**
     * 生成验证码
     */
    public function captcha($id){
        $builder = new CaptchaBuilder;//实例化验证码类
        $capth = $builder->build();//调用验证码类的build()方法
        $code = $builder->getPhrase();//得到生成的验证码的值
        session(['code'=>$code] );//将验证码的值存入session中
        //将验证码输出
        header('Content-type: image/jpeg');
        $builder->output();
    }

    /**
    *后台登录post请求检测方法
    *@return 后台登录模板
    */
    public function loginPost(Request $req){

        $users = DB::table('users')
            ->select('name', 'password','role')->get();
        foreach($users as $v){
            if($req->name == $v->name && md5($req->password) == $v->password && strtolower($req->captcha) == strtolower(session('code'))){
                 session(['member'=>$v]);//将用户信息写入session
                 return redirect('manager/');
            }
        }
        
        //已解决 
        echo "<script>alert('用户名或密码或验证码错误')</script>";
        return '<script>window.location.href=""</script>';
    }

    /**
     * 退出系统
     */
    public function logOut(){
        session(['member'=>'']);
        return redirect('manager/login'); 
    }

    /**
     * 生成动态功能菜单
     */
    private function initMenu(){
        /** 1.获取管理员信息 */
        $userinfo = session('member');
        $role_id = $userinfo -> role;
        /** 2. 获取管理员权限菜单 */
        /** 2.1获取管理员所有权限 */
        $user_per_list = RolePermission::
            where('rid',$role_id)
            ->leftJoin('permission','pid','=','per_id')
            ->select('cat_id','per_route','per_name')
            ->get();
        //dd($user_per_list);
        /** 2.2获取权限分组信息 */
        $per_group_list = Permission_group::orderBy('sort_order','desc') -> get();

        /** 2.3整合数据 */
        foreach($per_group_list as $key => $group){
            $data = [];
            foreach($user_per_list as $per){
                /** 判断### */
                if($per->per_route=="###"){
                    continue;
                }
                /** 判断用户权限分组 */
                if($per->cat_id == $group->pid){
                    $data[] = $per;
                }
            }
            $per_group_list[$key]->perList = $data;
        }
        return $per_group_list;
    }
}
